package com.google.code.bankagregator.entities;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Transient;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Deposit value object
 */
@Entity
public class Deposit extends Product implements Comparable<Deposit> {

    /**
     * Deposit rate
     */
    private Double depositRate;
    /**
     * Max money amount for product
     */
    private int maxAmount;
    /**
     * Min money amount for product
     */
    private int minAmount;
    /**
     * Deposit term (in number of days)
     */
    private int term;
    /**
     * is Refill possible
     */
    private boolean refill;
    @Enumerated(EnumType.STRING)
    private DepositType type;
    /**
     * Field to display <br/>
     * probable  match the search criteria and the conditions of the deposit</br>
     * its value calculated by the formula<br/>
     *
     * @todo
     */
    @Transient
    private Double delta;

    public Deposit() {

    }

    public Deposit(int id, String added, String currency, double depositRate, int minAmt, int maxAmt,
                   String name, int refill, String state,
                   int term, String dpType, String url, String bankName, String bankUrl) {

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-mm-dd HH:mm:ss");

        this.id = id;
        try {
            this.added = dateFormat.parse(added);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        this.depositRate = depositRate;
        this.currency = Currency.valueOf(currency);
        this.minAmount = minAmt;
        this.maxAmount = maxAmt;
        this.name = name;
        if (refill == 1) {
            this.refill = true;
        } else {
            this.refill = false;
        }
        this.state = State.valueOf(state);
        this.term = term;
        this.type = DepositType.valueOf(dpType);
        this.url = url;

        Bank bank = new Bank();
        bank.setName(bankName);
        bank.setUrl(bankUrl);

        this.bank = bank;
    }

    public Double getDepositRate() {
        return depositRate;
    }

    public void setDepositRate(Double depositRate) {
        this.depositRate = depositRate;
    }

    public int getMaxAmount() {
        return maxAmount;
    }

    public void setMaxAmount(int maxAmount) {
        this.maxAmount = maxAmount;
    }

    public int getTerm() {
        return term;
    }

    public void setTerm(int term) {
        this.term = term;
    }

    public boolean isRefill() {
        return refill;
    }

    public void setRefill(boolean refill) {
        this.refill = refill;
    }

    public int getMinAmount() {
        return minAmount;
    }

    public void setMinAmount(int minAmount) {
        this.minAmount = minAmount;
    }

    public DepositType getType() {
        return type;
    }

    public void setType(DepositType type) {
        this.type = type;
    }

    public Double getDelta() {
        return delta;
    }

    public void setDelta(Double delta) {
        this.delta = delta;
    }

    @Override
    public int compareTo(Deposit o) {
        return Double.compare(getDelta(), o.getDelta());
    }
}
